﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Billing.WindowsBase;
using Billing.DataObjects;

namespace Billing.Win
{
    public partial class AccountNumberPrefixEditForm : BaseEditor
    {
        eOpenMode mOpenMode;
        int mID;

        BillingDataContext mBillingDataContext;
        public BillingDataContext BillingDataContext
        {
            get
            {
                if (mBillingDataContext == null)
                    mBillingDataContext = new BillingDataContext();
                return mBillingDataContext;
            }
            set { mBillingDataContext = value; }
        }

        public AccountNumberPrefix CurrentObject
        {
            get { return bsEditor.DataSource as AccountNumberPrefix; }
            set { bsEditor.DataSource = value; }
        }

        public AccountNumberPrefixEditForm(eOpenMode OpenMode, int id)
            : base(OpenMode)
        {
            InitializeComponent();
            mOpenMode = OpenMode;
            mID = id;
        }

        protected override void onDataLoad()
        {
            base.onDataLoad();
            if (mOpenMode == eOpenMode.New)
            {
                bsEditor.DataSource = new AccountNumberPrefix();
                BillingDataContext.AccountNumberPrefixes.InsertOnSubmit(this.CurrentObject);
                this.CurrentObject.CreatedDate = DateTime.Now;
                this.CurrentObject.ModifiedDate = DateTime.Now;
            }
            else
            {
                var item = (from n in BillingDataContext.AccountNumberPrefixes
                            where n.ID == mID
                            select n).First();

                bsEditor.DataSource = item;
                this.CurrentObject.ModifiedDate = DateTime.Now;
            }
        }

        protected override void onSave()
        {
            BillingDataContext.SubmitChanges();
            DialogResult = DialogResult.OK;
            Close();
        }

        private void sbLBLTextBox1_ChooseBootonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            AccountNumberPrefixGroupForm form = new AccountNumberPrefixGroupForm(false);
            form.AllowAdd = false;
            form.AllowDelete = false;
            form.AllowEdit = false;
            form.AllowSave = false;
            form.AllowSelect = true;
            if (form.ShowDialog() == DialogResult.OK)
            {
                AccountNumberPrefixGroup accountNumberPrefix = ((AccountNumberPrefixGroup)form.SelectedObjects[0].CurrentObject);
                (bsEditor.DataSource as AccountNumberPrefix).AccountNumberPrefixGroup =
                    (from n in mBillingDataContext.AccountNumberPrefixGroups
                     where n.ID == accountNumberPrefix.ID
                     select n).First();
            }
        }

    }
}
